package com.twu.biblioteca.service;

import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.LibraryService;
import com.twu.biblioteca.model.ServiceResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by davecroman on 12/09/2014.
 */
public class CheckoutBookService extends LibraryService {
    private static String name = "Checkout Book";

    public CheckoutBookService(Library library){
        super(library);
    }

    @Override
    public ServiceResult performService() {
        System.out.print("Enter book id to checkout: ");
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

        try {
            String input = bufferRead.readLine();
            Integer bookId = Integer.parseInt(input);
            return performService(bookId);
        } catch (IOException e) {
            return new ServiceResult("Checkout Failed: Input Invalid");
        }
    }

    public ServiceResult performService(Integer bookId){
        try {
            library.checkOut(bookId, user);
        } catch (Exception e) {
            return new ServiceResult("That book is not available.");
        }
        return new ServiceResult("Thank you! Enjoy the book");
    }

    @Override
    public String getName() {
        return name;
    }

}
